package com.google.code.sip.ucenter.web.admin.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.core.utils.WebGridUtils;
import com.google.code.sip.ucenter.model.CreditType;
import com.google.code.sip.ucenter.service.CreditTypeManager;
import com.google.code.sip.ucenter.web.BaseController;

@Controller
public class CreditTypeController extends BaseController {

	@Autowired
	private CreditTypeManager creditTypeManager;

	@RequestMapping("/admin/creditType!list.do")
	public ModelAndView list(HttpServletRequest request) {
		Page<CreditType> page = new Page<CreditType>();
		WebGridUtils.fillPage(request, page);
		creditTypeManager.getAllCreditTypes(page);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("totalRows", (int) page.getTotalCount());
		model.put("page", page);
		return new ModelAndView("admin.creditType.list", model);
	}

	@RequestMapping("/admin/creditType!input.do")
	public ModelAndView input(HttpServletRequest request) {
		CreditType creditType = new CreditType();
		ModelAndView mv = new ModelAndView("admin.creditType.input");
		mv.addObject("creditType", creditType);
		System.out.println(request.getHeader("REFERER"));
		return mv;
	}

	@SuppressWarnings("deprecation")
	@RequestMapping("/admin/creditType!save.do")
	public ModelAndView save(
			@RequestParam(value = "iconfile", required = false) MultipartFile file,
			@ModelAttribute("CreditType") CreditType creditType,
			HttpServletRequest request) throws IOException {
		// 保存图标mediaFileName.substring(mediaFileName
		if (file.getSize() != 0) {
			String imageOriginalFileName = file.getOriginalFilename();
			String fileExtention = imageOriginalFileName
					.substring(imageOriginalFileName.lastIndexOf("."));
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyyMMddhhmmss");
			String newFileName = simpleDateFormat.format(new Date());
			String serverRootPath = request.getRealPath("/");
			String filePath = "/images/creditTypeIcon/" + newFileName
					+ fileExtention;
			String imagePath = serverRootPath + filePath;
			FileCopyUtils.copy(file.getBytes(), new File(imagePath));
			creditType.setImage(filePath);
		}

		creditTypeManager.saveCreditType(creditType);
		return list(request);
	}

	@RequestMapping("/admin/creditType!edit.do")
	public ModelAndView edit(@RequestParam("id") String id) {
		CreditType creditType = creditTypeManager.getCreditType(id);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("creditType", creditType);
		return new ModelAndView("admin.creditType.input", model);
	}

	@RequestMapping("/admin/creditType!delete.do")
	public ModelAndView delete(HttpServletRequest request,
			@RequestParam("id") String id) {
		if (creditTypeManager.isTransCreditType(id)) {
			saveError(request, "此积分已设置为交易积分，不能删除!");
			return list(request);
		}
		if (creditTypeManager.isCreditTypeUsed(id)) {
			saveError(request, "此积分已使用，不能删除!");
			return list(request);
		}
		creditTypeManager.deleteCreditType(id);
		return list(request);
	}
}
